//
// Created by Jisam on 05/10/2024 19:42.
// Solution of  d
#include <bits/stdc++.h>

#define  int long long
#define  endl "\n"
using namespace std;

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int a, b;
    cin >> a >> b;
    int c = 0;

    while (1) {
        if(__gcd(a + c, b + c) != 1ll){
//            cout << __gcd(a + c, b + c);
            cout << c;
            exit(0);
        }
        if(c > 1e5 && __gcd(a + c,b + c) == __gcd(b + c , 1ll) && __gcd(b + c , 1ll) == 1ll){
            cout << -1 <<endl;
            exit(0);
        }
        c ++;
    }
    return 0;
}